<?
/**
* @return void
* @param string $limit
* @param string $addparams
* @desc Information channel: storyline, articles, custom list
**/
if(!defined("common_extfunctions")) { define("common_extfunctions", "loaded");
include(ROOT_PATH."usr/system/common_extfunctions.inc.php"); }


// check input for where paramms

if(!defined("check_for_where_")){
    define("check_for_where_", "loaded");
    function check_for_where_($CHANNEL){
        global $env;
        if ($env['argv_nav'][2]=='filter'.$CHANNEL AND ($env['argv_nav'][3])) return true;
        return false;
    }
}


function get_infochannel($addparams=false){
	global $sapi_obj, $env, $http_path, $lang;
	
	global $root_path;
	
	$NavigationOffset = false;

	// Current environment placed in $sapi_obj->env

	// If the DDC parameter QUERY is passed over this CMS application then replace $addparams value with it
	if($sapi_obj->env["this.param.query"]) {
		$addparams = $sapi_obj->env["this.param.query"];
		$sapi_obj->env["this.param.query"] = false;
	}

	// Parse all queries of the DDC
	$date_qc = "";
	if($GLOBALS["this_sapi"]["exec"]) {
		preg_match_all("/<sapi:apply\s+name=\"qc\.([a-zA-Z_\d\.\-]+)\.value\"([^>]+)>/is", $GLOBALS["this_sapi"]["exec"], $matches);
		if($matches) {
			foreach($matches[1] as $index => $value) {
				$type = " ";
				if($matches[2][$index]) {
					if(preg_match("/type=\".*?\"/is", $matches[2][$index]))
					$type = preg_replace("/^.*?type=\"(.*?)\".*?$/is", "\\1", $matches[2][$index]);

					if(preg_match("/index=\".*?\"/is", $matches[2][$index])) {
						$qc_index_list [ $value ] = $type;
						if($type=="date") $date_qc = $value;
					}
				}
				$qc_list [ $value ] = $type;
			}
		}
	}

	// DATE_CREATE correction. If there is index date parameter system DATA_CREATE will be based on it
	if($date_qc) {
		if($_POST[$date_qc] AND defined("PHP_DATEFORMAT")) {
			switch(PHP_DATEFORMAT) {
				case "d/m/Y":
				list($d,$m,$y) = split("\/", $_POST[$date_qc]);
				break;
				case "d/m/y":
				list($d,$m,$y) = split("\/", $_POST[$date_qc]);
				$y = ($y > 60 ? 1900 : 2000) + $y;
				break;
				case "m/d/Y":
				list($m,$d,$y) = split("\/", $_POST[$date_qc]);
				break;
				case "m/d/y":
				list($m,$d,$y) = split("\/", $_POST[$date_qc]);
				$y = ($y > 60 ? 1900 : 2000) + $y;
				break;
				case "Y-m-d":
				list($y,$m,$d) = split("-", $_POST[$date_qc]);
				break;
				case "Y/m/d":
				list($y,$m,$d) = split("/", $_POST[$date_qc]);
				break;
			}
			$data_create_date = "$y-$m-$d ".date("H:i:s");
		}
	}


	// Analize input parameters
	
	if( preg_match("/_CHANNEL\(/s", $addparams) )  {
		preg_match("/(?<=_CHANNEL\().+?(?=\)_)/s",$addparams, $CHANNEL);
		$CHANNEL=$CHANNEL[0];
	}
	
	// infochannel param (_NAVURL_) which controls the the path of navigation independently of the (_BASEURL_)
	if( preg_match("/_NAVURL\(/s", $addparams) ) $NAVURL =  preg_replace("/^.*?_NAVURL\((.*?)\)_.*?$/s", "\\1", $addparams);
	

	if( preg_match("/_BASEURL\(/s", $addparams) ) $BASEURL =  preg_replace("/^.*?_BASEURL\((.*?)\)_.*?$/s", "\\1", $addparams);


	// maxTextlength,closeTextString,filterRow are new infochannel params from now _
	if( preg_match("/_FILTERROW\(/s", $addparams) ) $FILTERROW =  preg_replace("/^.*?_FILTERROW\((.*?)\)_.*?$/s", "\\1", $addparams);
	if( preg_match("/_MAXTEXTLENGTH\(/s", $addparams) ) $MAXTEXTlength =  preg_replace("/^.*?_MAXTEXTlength\((.*?)\)_.*?$/s", "\\1", $addparams);
	if( preg_match("/_CLOSETEXTSTRING\(/s", $addparams) ) $CLOSETEXTSTRING =  preg_replace("/^.*?_CLOSETEXTSTRING\((.*?)\)_.*?$/s", "\\1", $addparams);




	if( preg_match("/_WHERE\(/s", $addparams) ) {
		$WHERE =  preg_replace("/^.*?_WHERE\((.*?)\)_.*?$/s", "\\1", $addparams);
	} elseif (check_for_where_($CHANNEL)) {
		$addparams = $addparams."_WHERE( this.this.".strtoupper($FILTERROW).".value like ".$env['argv_nav'][3]."% )_";
		$WHERE =  preg_replace("/^.*?_WHERE\((.*?)\)_.*?$/s", "\\1", $addparams);
	}


	if( preg_match("/_ORDER\(RANDOM\)_/s", $addparams) ) $ORDER = "random";
	if( preg_match("/_ORDER\(DATEC,\s?ASC\)_/s", $addparams) ) $ORDER = "datecasc";
	if( preg_match("/_ORDER\(DATEC,\s?DESC\)_/s", $addparams) ) $ORDER = "datecdesc";
	if( preg_match("/_ORDER\(INDEX,\s?([^,]+),\s?ASC\)_/s", $addparams) ) {
		$sapi_obj->env["index"] = preg_replace("/^.*?_ORDER\(INDEX,\s?([^,]+),\s?ASC\)_.*?$/s", "\\1", $addparams);
		$ORDER = "index_asc";
	}
	if( preg_match("/_ORDER\(INDEX,\s?([^,]+),\s?DESC\)_/s", $addparams) ) {
		$sapi_obj->env["index"] = preg_replace("/^.*?_ORDER\(INDEX,\s?([^,]+),\s?DESC\)_.*?$/s", "\\1", $addparams);
		$ORDER = "index_desc";
	}


	if( preg_match("/_LIMIT\([^\)]+\)_/s", $addparams) ) {
		preg_match_all("/_LIMIT\((\d+),(\d+)\)_/s", $addparams, $matches);
		$limit_start=$matches[1][0];
		$limit = $matches[2][0];
		$limit_finish=$limit_start+$matches[2][0]-1;
	}
	
	if( preg_match("/^rowselect-/is", $env["argv_nav"][1]) ) $NavigationOffset = $env["argv_nav"][1]."/";

	$local_http_path = $sapi_obj->env["http_path"];
	if($GLOBALS["MODREWRITE"]=="MultiviewsMode") $local_http_path = $http_path;

	if($BASEURL) $baseurl = $BASEURL; else $baseurl=$sapi_obj->env["argv_string"];
	if( @preg_match("/^\//", $baseurl) ) $baseurl = $local_http_path.substr($baseurl, 1);
	elseif( !@preg_match("/^http:/", $baseurl) ) $baseurl = $local_http_path.$baseurl;


	if(!$CHANNEL) $CHANNEL = "news";
	$filename=$GLOBALS["root_path"] . "usr/xml/vdb/".$CHANNEL.".xml";
	$channelpath=$GLOBALS["root_path"] . "usr/xml/vdb/".$CHANNEL;

	$sapi_obj->env["this.channel"]= $CHANNEL;

	// If row selection is sent in the address string
	if($env["argv_nav"][1] AND $env["argv_nav"][0]==$CHANNEL) {
		// FORMAT: http://localhost/item1/data/rowselect-0-10/
		if( preg_match("/^rowselect-/is", $env["argv_nav"][1]) ) {
			$row_selection = preg_replace("/^rowselect-/is", "", $env["argv_nav"][1]);
			list($limit_start, $limit) = split("-", $row_selection);
			$limit_finish=$limit_start+$limit-1;
		}
	}

	// Get rows of the channel
	if (!isset($db)) $db = new sapi_vdb($filename);

	 
	if (!isset($env["argv_virtual.id"]) && is_numeric($env["argv_nav"][1]) ) $sapi_obj->env["argv_virtual.id"]=$env["argv_virtual.id"]=$env["argv_nav"][1];

	/****
	** Save into VDB
	****/
	

	//    if(!$flag_upload AND $env["area51"] and $_POST["container"]["qc"] and $_POST["post_action"]!="ADDNEWCOMMENT"
	//    and ($env["argv_nav"][0]=="addentry" OR $sapi_obj->env["page"]["MASK"]) ) {

	
	// Fixed news cloning
	if($env["argv_nav"][0]=="addentry" AND $env["argv_nav"][1]!=$CHANNEL  ) $flag_upload=1;

	if(!$flag_upload AND $env["area51"] and $_POST["container"]["qc"] and $_POST["post_action"]!="ADDNEWCOMMENT") { //test

	// If there are DATA in POST
	$flag_there_are_data = false;
	foreach($qc_index_list as $qc => $type) {
		if( $_POST[$qc] ) $flag_there_are_data = true;
	}



	//

	if($flag_there_are_data) {

		// Get index tree
		$stream=$db->get_data();
		// Row ID - $rn["argv_virtual.id"]
		if($sapi_obj->env["argv_virtual.id"]>0)
		$data["post_action"] = "UPDATEROW";
		else
		$data["post_action"] = "ADDROW";

		if(!$stream) $db->new_id = 1;

		// Delare index fields
		$data["row"]["ID"] = $sapi_obj->env["argv_virtual.id"];
		if($data_create_date || $stream[$data["row"]["ID"]]['DATE_CREATE']) $data["row"]["DATE_CREATE"] =($stream[$data["row"]["ID"]]['DATE_CREATE'])?$stream[$data["row"]["ID"]]['DATE_CREATE']:$data_create_date;
		if($qc_index_list) {
			foreach($qc_index_list as $qc => $type) {
				$data["row"][$qc] = $_POST[$qc];
			}
		}


		// Put indexes into VDB
		$db->save_data_row(false, $data);

		// Put data into certain file
		if( !is_dir($channelpath) ) {
			if(!mkdir($channelpath, 0777))
			message_die("Can not create the ".$channelpath);
		}

		if (!isset($dbp)) {
			$dbp =new sapi_vdb($channelpath."/".($sapi_obj->env["argv_virtual.id"]?$sapi_obj->env["argv_virtual.id"]:$db->new_id).".xml");
		}

		$data = array();

		$content = $dbp->get_data();
		$data = $content[1];
		if($qc_list) {
			foreach($qc_list as $qc => $type) {
				if(isset($_POST[$qc])) $data[$qc] = $_POST[$qc];
			}
		}

		$data["ID"] = ($sapi_obj->env["argv_virtual.id"]?$sapi_obj->env["argv_virtual.id"]:$db->new_id);
		$dbp->save_objdata(array($data));
		$dbp->close();

		$db->close();
		
		// Redirection to infochannel general page
		if($env["argv_nav"][0]=="addentry") {
			if($MODREWRITE=="disabled")
				header("Location: ".HTTP_PATH."vpath=".$env["argv_string"]);
			else
				header("Location: ".$env["document.url"]);
		}
		
	}
	}

	// Delete entry
	if($env["area51"] and $env["argv_nav"][0]=="deleteentry" and $env["argv_nav"][1]) {
		@unlink($channelpath."/".$env["argv_nav"][1].".xml");
		$db->delete_data_row($env["argv_nav"][1], "ID");
		$db->close();
		$stream=$db->get_data();
	}

	// If "addentry" interface is selected
	if($env["argv_nav"][0]=="addentry" AND ( !isset($env["argv_nav"][1]) OR $env["argv_nav"][1]==$CHANNEL ) )
	return $stream=array("0"=>array("TEXT"=>"Nothing to display"));

	if(isset($sapi_obj->env["virtual_path_mask"])) {
		$arr=explode("/", preg_replace("/\/$/", "", $sapi_obj->env["virtual_path_mask"]));
		$filter["DATE_CREATE"]=($arr[0]?$arr[0] . "-":"") . ($arr[1]?$arr[1] . "-":"") . ($arr[2]?$arr[2]:"");
		$filter["ID"]=($arr[3]?$arr[3]:"");
		$db->set_filter($filter);
	}

	// If it was reported address string argument IC_CHANNELNAME, then we open chosen entry
	if( preg_match("/^ic_/is", $env["argv_nav"][0]) ) {
		$stream=$db->get_data($channelpath."/".$env["argv_nav"][1].".xml");
	} else  $stream=$db->get_data();


	$db->close();



	if($stream) {

		// Analize WHERE
		if($WHERE) {
			$counter = 0;
			foreach($stream as $inx => $f) {
				if($f) {
					foreach($f as $k => $v)	 {
						$sapi_obj->env["this.this.".$k] = $v;
					}
				}
				if($env) {
					$WHERE_TEMPLATE = $WHERE;
					
					foreach( $sapi_obj->env as $key=>$val) {
						
						if( !is_array($val) ) {
							// Parse LIKE constructions
							if( preg_match("/like\s([^\s]+)\s/is", $WHERE_TEMPLATE )) {
								$WHERE_TEMPLATE = preg_replace("/\s([^\s]+)\slike\s([^\s]+)\s/is", " preg_match('/^\\2\$/is', \\1) ", $WHERE_TEMPLATE );
								$WHERE_TEMPLATE = str_replace("%", '.*?', $WHERE_TEMPLATE);
							}

							if(is_string($val)) $WHERE_TEMPLATE = str_replace($key.".value", "'".addslashes($val)."'", $WHERE_TEMPLATE);
							else  $WHERE_TEMPLATE = str_replace($key.".value", addslashes($val), $WHERE_TEMPLATE);
						}
					}
				}

				@eval("\$res=".$WHERE_TEMPLATE.";");
				if(!$res) unset($stream[$inx]);
				$counter++;
			}
			// Clear ENV
			foreach($f as $k => $v)	 {
				//                $sapi_obj->env["this.this.".$k] = $v;
				unset ( $sapi_obj->env["this.this.".$k] );
			}
		}

		$channel_length = count($stream);


		if($ORDER == "datecasc") usort ($stream, "cmp_datecasc");
		if($ORDER == "datecdesc") usort ($stream, "cmp_datecdesc");
		if($ORDER == "random") usort ($stream, "cmp_random");
		if($ORDER == "index_asc") usort ($stream, "cmp_indexasc");
		if($ORDER == "index_desc") usort ($stream, "cmp_indexdesc");

		
		

		// Get content
		$counter = 0;
		foreach($stream as $inx => $f) {
			if((!isset($limit_start) OR $counter>=$limit_start) AND (!isset($limit_finish) OR $counter<=$limit_finish)) {
				$fetch = $db->get_data($channelpath."/".($f["ID"]).".xml", "_ONEROW_");

				if($f["DATE_CREATE"]) $fetch["DATE_CREATE"] = $f["DATE_CREATE"];

				// Upload control
				if($fetch) {
					foreach($fetch as $inx1 => $val1) {
						if($upload_data[$inx1]) $fetch[$inx1] = $upload_data[$inx1];
					}
				}

				if($fetch) $stream[$inx] = $fetch; else unset($stream[$inx]);
			} else unset($stream[$inx]);

			$counter++;
		}
		
	}
	

	
	$counter=0;
	

	if (is_array($stream))
	foreach ($stream as $key=>$value) {
		$sapi_obj->env["infochannelfilterdata.year"][ substr($value["DATE_CREATE"],0,4) ] = true;
		$sapi_obj->env["infochannelfilterdata.month"][ substr($value["DATE_CREATE"],5,2) ] = true;
		$sapi_obj->env["infochannelfilterdata.day"][ substr($value["DATE_CREATE"],8,2) ] = true;

		$date=date("Y/m/d", strtotime($value["DATE_CREATE"]));
		$counter++;

		// Enumeration's entry
		if (count($stream)>1) {
			
			$maxTextlength=($MAXTEXTlength)?$MAXTEXTlength:400;
			$aspace=".";
			$text=$value["TEXT"];
			if(strlen($text) > $maxTextlength ) {
				$text = substr(trim($text),0,$maxTextlength);
				$text = substr($text,0,strlen($text)-strpos(strrev($text),$aspace));
				$text = $text.($CLOSETEXTSTRING)?$CLOSETEXTSTRING:'';
			}
			
			$stream[$key]["TEXT"]=$value["TEXT"];
			$stream[$key]["CLEANTEXT"]=preg_replace("/\&([a-zA-Z]+)\;/is", " ", strip_tags(substr($value["TEXT"], 0, 400) . "..."));
			$stream[$key]["MORE"]= $baseurl . $date . '/' . ($stream[$key]["ID"]) . "/";
			$stream[$key]["COUNTER"]=$counter;
			$stream[$key]["to_select"] = $baseurl . 'data/ic_' . $CHANNEL . '/' . ($stream[$key]["ID"]) . '/'.$NavigationOff;
			$stream[$key]["TO_SELECT"] = $stream[$key]["to_select"].$NavigationOff;

		} else {
			// Open entry
			
			$stream[$key]["to_select"] = $baseurl . 'data/ic_' . $CHANNEL . '/' . ($stream[$key]["ID"]) . '/'.$NavigationOff;
			
			$stream[$key]["DATE_CREATE"]=$value["DATE_CREATE"];
			$stream[$key]["IP"]=$value["IP"];
			$stream[$key]["TEXT"]=$value["TEXT"];
			$stream[$key]["MORE"]= $baseurl . $date . '/' . ($stream[$key]["ID"]) . "/";
			$stream[$key]["TITLE"]=$value["TITLE"];
			
			$stream[$key]["to_select"] = $baseurl . 'data/ic_' . $CHANNEL . '/' . ($stream[$key]["ID"]) . '/'.$NavigationOff;
			$stream[$key]["TO_SELECT"] = $stream[$key]["to_select"].$NavigationOff;			
			$stream[$key]["CLEANTEXT"]=preg_replace("/\&([a-zA-Z]+)\;/is", " ", strip_tags($value["TEXT"]));
			if ($sapi_obj->env["virtual_path_mask"] OR preg_match("/^ic_/is", $env["argv_nav"][0])) $stream[$key]["LESS"]= $baseurl;
			$stream[$key]["COUNTER"]=$counter;
			
			$stream[$key]["to_select"] = $baseurl . 'data/ic_' . $CHANNEL . '/' . ($stream[$key]["ID"]) . '/'.$NavigationOff;
			$stream[$key]["TO_SELECT"] = $stream[$key]["to_select"].$NavigationOff;
			

			
			if($qc_list) {
				foreach($qc_list as $qc => $type) {
				
					if(!$sapi_obj->env["inf.".$qc]) $sapi_obj->env["infoChannel.".strtolower($qc)] = $sapi_obj->env["infoChannel.".$qc] = $sapi_obj->env["infoChannel"][$qc] = $value[$qc];
					
				}
			}

			// Delete certain image
			if($_POST) {
				foreach($_POST as $index => $value) {
					if(substr($index,0,7)=="delete_" AND $value==1) {
						@unlink($GLOBALS["root_path"] . "usr/templates/" . $_POST["backup_".substr($index,7)] );
						$_POST["backup_".substr($index,7)]="";
						$_POST[substr($index,7)]="";
						$sapi_obj->env["page.".substr($index,7)]="";
					}
				}
			}


		}
		

		if($qc_list) {
			foreach($qc_list as $qc => $type) {
				if(!isset($sapi_obj->env["infoChannel.".$qc]))  $stream[$key][strtolower($qc)] = $stream[$key][$qc];
				if($type=="image") { 
					if(!$stream[$key][$qc]) { 
						$stream[$key][$qc] = "images/x.gif";
						$stream[$key]["icon_".strtolower($qc)] = $stream[$key]["ICON_".strtoupper($qc)] = "images/x.gif"; } 
						else {
						$_icon = preg_replace("/^images\//is", "images/icon_", $stream[$key][$qc]);
						$stream[$key]["icon_".strtolower($qc)] = $stream[$key]["ICON_".strtoupper($qc)] = $_icon;
						}
				}
				
			}
		}



	}else{
		$stream=array("0"=>array("TEXT"=>"Nothing to display"));
	}
	
	
	
	// an new infochannel param (_NAVURL_) which controls the the path of navigation independently of the _BASEURL_
	if (isset($NAVURL)) {
		$tmpBaseUrl=$baseurl;
		$baseurl=$local_http_path.$NAVURL;
	}

	if (count($stream)==1) {

		$_cur = current($stream);
		$_ID = $_cur["ID"];
		$_key = key($stream);
		$db->close();

		// Calculation of previous and next pointers
		$index_tree=$db->get_data($GLOBALS["root_path"]."usr/xml/vdb/".$CHANNEL.".xml");
		if($index_tree) {
			usort ($index_tree, "idsort");
			reset($index_tree);
			while(current($index_tree)) {
				$fetch = current($index_tree);
				if($fetch["ID"]==$env["argv_nav"][1]) break;
				next($index_tree);
			}
			$stream[$_key]["prev_to_select"] = false;
			$stream[$_key]["next_to_select"] = false;
			$stream[$_key]["prev_display"] = "none";
			$stream[$_key]["next_display"] = "none";
			if($_ID>1) {
				prev($index_tree);
				$_prev = current($index_tree);
				$stream[$_key]["prev_to_select"] = $baseurl . 'data/ic_' . $CHANNEL . '/' . ($_prev["ID"]) . '/';
				$stream[$_key]["prev_display"] = "block";
				next($index_tree);
			}
			next($index_tree);
			$_next = current($index_tree);
			if($_next) {
				$stream[$_key]["next_to_select"] = $baseurl . 'data/ic_' . $CHANNEL . '/' . ($_next["ID"]) . '/';
				$stream[$_key]["next_display"] = "block";
			}
		}
	}




	$counter = 0;

	// Sheiko's notice: One should resort stream before doing limitation cut
	if(!$ORDER) $ORDER="udate";
	if($ORDER == "udate") usort ($stream, "udatesort");


	// Analize of news archive navigation
	$sapi_obj->env["this.navigation.next.style.display"]=$sapi_obj->env[$CHANNEL.".navigation.next.style.display"]="none";
	$sapi_obj->env["this.navigation.previous.style.display"]=$sapi_obj->env[$CHANNEL.".navigation.previous.style.display"]="none";
	//   param for the navsystem next/ prev  limitNumber
	$sapi_obj->env["this.navigation.limitNumber"]=$sapi_obj->env[$CHANNEL.".navigation.limitNumber"]=$limit;
	

	if(!$limit_finish) $limit_finish = $limit; if($limit==1 && $limit_start!=0) $channel_length--;

 
	if(($channel_length>$limit AND !$limit_finish) OR $channel_length>$limit_finish+1) {
		$sapi_obj->env["this.navigation.next"]=$sapi_obj->env[$CHANNEL.".navigation.next"]=1;
		$sapi_obj->env["this.navigation.next.style.display"]=$sapi_obj->env[$CHANNEL.".navigation.next.style.display"]="block";
		if($limit_start==0 && $limit==1)
		$sapi_obj->env["this.navigation.next.href"]=$sapi_obj->env[$CHANNEL.".navigation.next.href"]= $baseurl."data/$CHANNEL/rowselect-1-1/".( $sapi_obj->env["argv_nav"][2]=="filter"?"filter/".$sapi_obj->env["argv_nav"][3]."/":"" );
		else
		$sapi_obj->env["this.navigation.next.href"]=$sapi_obj->env[$CHANNEL.".navigation.next.href"]= $baseurl."data/$CHANNEL/rowselect-".((int)$limit_finish+1)."-".($limit)."/".( $sapi_obj->env["argv_nav"][2]=="filter"?"filter/".$sapi_obj->env["argv_nav"][3]."/":"" );
	}

	if((!($limit==1 && $limit_start==0)) && ($limit_start>=$limit-1)) { $beg =$limit_start - $limit; if($beg<0) $beg=0;
	$sapi_obj->env["this.navigation.previous"]=$sapi_obj->env[$CHANNEL.".navigation.previous"]=1;
	$sapi_obj->env["this.navigation.previous.style.display"]=$sapi_obj->env[$CHANNEL.".navigation.previous.style.display"]="block";
	$sapi_obj->env["this.navigation.previous.href"]= $sapi_obj->env[$CHANNEL.".navigation.previous.href"]= $baseurl."data/$CHANNEL/rowselect-".((int)$beg)."-".($limit)."/".( $sapi_obj->env["argv_nav"][2]=="filter"?"filter/".$sapi_obj->env["argv_nav"][3]."/":"" );
	}
	 // a new infochannel param (_NAVURL_) which controls the the path of navigation indepently of the (_BASEURL_) // restore to original state
	if (isset($NAVURL))  $baseurl=$tmpBaseUrl;
	
	
	$sapi_obj->env["this.navigation.add.href"] = $local_http_path.($GLOBALS["MODREWRITE"]=="disabled"?"?vpath=":"").$sapi_obj->env["argv_string"]."data/addentry/".$CHANNEL."/";
	
	$sapi_obj->env["this.navigation.list.href"] = $sapi_obj->env["document.url"].($sapi_obj->env["argv_nav"][2]?"data/".$CHANNEL."/".$sapi_obj->env["argv_nav"][2]."/":"");
	
	$StatusMode = array( 0 => "Delivery",  1 => "Administration", 2 => "Editing");

	if($stream) {
		$__counter = 0;
		$sapi_obj->env["this.length"] = 
		$sapi_obj->env["this.lenght"] = count($stream);
		foreach ($stream as $key => $fetch) {
			$stream[$key]["counter"] = ++$__counter;
			
			$stream[$key]["Status"] = (!$sapi_obj->env["page.MASK"]?($stream[$key]["MORE"]?"As a list":"As a list without link"):"As a document");
			$stream[$key]["Mode"] = $StatusMode[$sapi_obj->env["area51"]];
			$stream[$key]["EditingMode"] = ($sapi_obj->env["area51"]==2?"On":"Off");
			if(
			$sapi_obj->env["argv_nav"][0]=="addentry" AND 
			$sapi_obj->env["argv_nav"][1]==$CHANNEL AND 
			$sapi_obj->env["area51"]>1 ) $stream[$key]["AddButtonVisibility"]="Showed";
			else $stream[$key]["AddButtonVisibility"]="Hidden";
		}
	}
	/*
	Values of this.this.Status should be:
		As a list
		As a list without link
		As a document
	
	Values of this.this.Mode should be:		
		Delivery
		Administration
		Editing
		
	Values of this.this.EditingMode should be:		
		On
		Off

	Values of this.this.AddButtonVisibility should be:		
		Showed
		Hidden
		
	*/
	
	return $stream;
}

if(!defined("udatesort")) {
    define("udatesort", "loaded");
    function udatesort($a, $b){
        if($a["DATE"]==$b["DATE"]) {
            if( $a["DATE_CREATE"] == $b["DATE_CREATE"] ) return 0;
            if(strtotime($a["DATE_CREATE"])>strtotime($b["DATE_CREATE"])) return -1; else return 1;
        }
        if(strtotime($a["DATE"])>strtotime($b["DATE"])) return -1; else return 1;
    }
}

if(!defined("idsort")) {
    define("idsort", "loaded");
    function idsort($a, $b){
        return ($a["ID"] < $b["ID"])? -1:1;
    }
}
?>